//
//  ViewController.m
//  RunCloundA
//
//  Created by ChenXiaWen on 16/4/5.
//  Copyright © 2016年 ChenXiaWen. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController

@synthesize frac1,tableData;
static ViewController *_instance;

+(ViewController*) instance
{
    return _instance;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"simpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // [[self tableData] addObject:item];
    NSString *heroSelected=[tableData objectAtIndex:indexPath.row];
    //indexPath.row得到选中的行号，提取出在数组中的内容。
    UIAlertView *myAlertView;
    myAlertView = [[UIAlertView alloc]initWithTitle:@"连接选择的设备" message:heroSelected delegate:self cancelButtonTitle:@"好的" otherButtonTitles:@"取消", nil];
    [myAlertView show];
    [tableView deselectRowAtIndexPath:indexPath animated:false];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex is : %i",buttonIndex);
    switch (buttonIndex) {
        case 0:{
            // 好的, 开始连接蓝牙设备
            [frac1 connectDevice];
        }break;
        case 1:{
            // 取消
        }break;
        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    frac1 = [[ Fraction alloc] init];
    _instance = self;
    tableData = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doClick:(id)sender {
    NSLog(@"my message");

    [frac1 print];
}

- (IBAction)doClickTest:(id)sender {
    NSLog(@"doClickTest");
 
    [[self tableData] addObject:@"0001"];
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [indexPaths addObject: indexPath];
  
    
    [[self tableview] beginUpdates];
    [[self tableview] insertRowsAtIndexPaths:indexPaths withRowAnimation:	UITableViewRowAnimationRight];
    [[self tableview] endUpdates];
 
}


-(void)AddListItem:(NSString*)item
{
    [[self tableData] addObject:item];
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[[self tableData] count]-1 inSection:0];
    [indexPaths addObject: indexPath];
    
    [[self tableview] beginUpdates];
    [[self tableview] insertRowsAtIndexPaths:indexPaths withRowAnimation:	UITableViewRowAnimationRight];
    [[self tableview] endUpdates];
}

@end
